import { Command, CommanderStatic } from 'commander';
import { IAction, LinkModelAction } from '../../actions';
import { CommandBase } from '../command-base/command-base';
import { Input } from '../../core';

export class LinkModelCommand extends CommandBase {
  /**
   * 加载命令
   *
   * @author chitanda
   * @date 2021-04-18 13:04:43
   * @abstract
   * @param {CommanderStatic} program
   */
  protected load(program: CommanderStatic): void {
    program
      .command('link-model')
      .description('链接当前应用的模型')
      .option('-m, --model-path [path]', '模型所在目录，默认为项目根目录下[model]文件夹。')
      .option('-n, --name [name]', '应用名称，默认从应用的文件夹截取。')
      .option('-f, --force', '当指向的目标文件存在时，删除原有[文件 or 文件夹]并建立软链接!')
      .action(async (options: Record<string, string | boolean>, _command: Command) => {
        const opts: Input = {};
        opts.force = { name: 'force', value: options.force };
        opts.modelPath = { name: 'modelPath', value: options.modelPath || 'model' };
        opts.name = { name: 'name', value: options.name || null };
        try {
          await this.action.handle({}, opts);
        } catch (err) {
          process.exit(0);
        }
      });
  }

  /**
   * 生成行为
   *
   * @author chitanda
   * @date 2021-04-18 13:04:04
   * @protected
   * @abstract
   * @return {*}  {IAction}
   */
  protected getAction(): IAction {
    return new LinkModelAction();
  }
}
